package com.pagepub.expo.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import com.pagepub.expo.entity.AppFunction;
import com.pagepub.expo.entity.AppRole;
import com.pagepub.expo.entity.CorpMenu;
@Repository
public class CorpMenuDao {
	@Autowired
	@Qualifier("jdbcTemplate")
	private JdbcTemplate template;
	
	@Autowired
	@Qualifier("appSeqDao")
	private AppSeqDao appSeqDao;
	
	private static String tableName="Corp_Menu";
	
	
	
	
	private static String QUERY_ALL = "SELECT id,menuName,menuUrl,parentId,menuLevel,menuOrder,isValid FROM "+tableName;

	private static String QUERY_ALL_COUNT = "SELECT COUNT(*) FROM "+  tableName;
	
	private static String INSERT_SQL = "INSERT  INTO "+tableName+" (" +
			"id,menuName,menuUrl,parentId,menuLevel,menuOrder,isValid)VALUES(" +
			":id,:menuName,:menuUrl,:parentId,:menuLevel,:menuOrder,:isValid)";

	private static String DELETE_SQL = "DELETE FROM "+ tableName +" WHERE id=?";

	private static String UPDATE_SQL = "UPDATE "+ tableName + " SET " +
			"menuName=:menuName,menuUrl=:menuUrl,parentId=:parentId,menuLevel=:menuLevel,menuOrder=:menuOrder,isValid=:isValid"+
			"WHERE id=:id";

	private final ParameterizedRowMapper<CorpMenu> mapper = new ParameterizedRowMapper<CorpMenu>() {
		public CorpMenu mapRow(ResultSet rs, int rowNum) throws SQLException {
			CorpMenu obj = new CorpMenu();
			obj.setId(rs.getLong("id"));
			obj.setMenuName(rs.getString("menuName"));
			obj.setMenuUrl(rs.getString("menuUrl"));
			obj.setParentId(rs.getLong("parentId"));
			obj.setMenuLevel(rs.getLong("menuLevel"));
			obj.setMenuOrder(rs.getLong("menuOrder"));
			obj.setIsValid(rs.getLong("isValid"));
			return obj;
		}
	};

	public List<CorpMenu> findAll() {
		return template.query(QUERY_ALL, mapper);
	}

	public CorpMenu findById(Long id) {
		List<CorpMenu> list = template.query(QUERY_ALL + " WHERE functionId=?",
				new Object[] { id }, mapper);
		if (list.size() == 0) {
			return null;
		} else
			return list.get(0);
	}
	
	
	public List<CorpMenu>  findByParentId(Long id) {
		return template.query(QUERY_ALL + " WHERE parentId=?",	new Object[] { id }, mapper);
		
	}
	
	public List<CorpMenu> findByCorpTypeId(Long corpTypeId) {
		String sql = "select t1.id,menuName,menuUrl,parentId,menuLevel,menuOrder,isValid " +
				"from corp_menu t1 , corp_menu_type t2 where t1.id=t2.menuid and t1.menulevel=1 and t2.corptypeid ="+ corpTypeId;

		sql+=" order by t1.menuOrder desc ";
		System.out.println(sql);
		List<CorpMenu> parentList = template.query(sql , mapper);
		for(int j=0;j<parentList.size();j++){
			parentList.get(j).setSublist(findByParentId(parentList.get(j).getId()));
		}
		return parentList;
	}
	
	
	
	
	

	
	public void insert(CorpMenu bean) {
		if(bean.getId()==null){
			bean.setId(new Long(appSeqDao.getSequence("corp_menu_seq")));
		}			
		SqlParameterSource ps = new BeanPropertySqlParameterSource(bean);
		NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(
				template.getDataSource());
		npTemplate.update(INSERT_SQL, ps);

	}

	public void delete(Long id) {
		template.update(DELETE_SQL, id);
	}

	public void update(CorpMenu bean) {
		SqlParameterSource ps = new BeanPropertySqlParameterSource(bean);
		NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(
				template.getDataSource());
		npTemplate.update(UPDATE_SQL, ps);
	}
	
	public int getCount(Map<String, String> param){
		int count = template.queryForInt(QUERY_ALL_COUNT);
		return count;
	}
	
	public List findByPage(int currentPage,int pageSize,Map<String, String> param){
		List lists = template.query(QUERY_ALL + " limit ?,?",
				new Object[] { currentPage, pageSize}, mapper);
		return lists;
		
	}
}
